package com.pactera.madp.cp.controller.oms.employee;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.em.workemployee.EmployeeStatusEnum;
import com.pactera.madp.cp.api.entity.WorkEmployeeEntity;
import com.pactera.madp.cp.api.vo.contact.ContactInheritVO;
import com.pactera.madp.cp.api.vo.employee.EmployeeInheritVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.EmployeeInheritService;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工离职继承、在职继承客户或者客户群
 *
 * @author yushen.qi
 * @since 2021-07-19 15:04:33
 */
@RestController
@AllArgsConstructor
@RequestMapping("/inheritemployee")
@Api(value = "inheritemployee", tags = "在离职继承管理")
public class EmployeeInheritController {

    @Resource
    private EmployeeInheritService employeeInheritService;
    @Autowired
    private IWorkEmployeeService employeeService;

    @GetMapping("/page")
    @ApiOperation(value = "获取当前企业所有成员")
    public R<IPage<EmployeeInheritVO>> getListByChoose(Page page, String name, String mobile , Integer status){
        if(StrUtil.isNotBlank(name) && name.length() > 20){
            return R.failed("参数不合法，名称查询参数不能多于20个字符！");
        }
        if (StrUtil.isNotBlank(mobile) && mobile.length() > 11) {
            return R.failed("参数不合法，手机号模糊查找应小于等于11位！");
        }
     return R.ok(employeeInheritService.getListByChoose(page, name, mobile, status));
    }

    @GetMapping("/contact")
    @ApiOperation(value = "查询员工名下客户")
    public R<IPage<ContactInheritVO>> getContactByUser(@RequestParam(value = "id") Integer id, Page page){
        return R.ok(employeeInheritService.getContactByUser(id,page));
    }

    @GetMapping("/name")
    @ApiOperation(value = "根据名称查询已激活的具有外部联系人权限的企业成员")
    public R<List<EmployeeInheritVO>> getUserByName(@RequestParam(value = "name") String name){
            List<EmployeeInheritVO> collect = employeeService.list(Wrappers.<WorkEmployeeEntity>lambdaQuery().like(WorkEmployeeEntity::getName, name).eq(WorkEmployeeEntity::getStatus, 1).eq(WorkEmployeeEntity::getContactAuth,1).eq(WorkEmployeeEntity::getCorpId, CorpContextHolder.getCorpId())).stream().map(entity -> {
                EmployeeInheritVO employeeInheritVO = new EmployeeInheritVO();
            BeanUtil.copyProperties(entity, employeeInheritVO);
            employeeInheritVO.setStatus(EmployeeStatusEnum.getMsgByCode(entity.getStatus()));
            return employeeInheritVO;
        }).collect(Collectors.toList());
        return R.ok(collect) ;
    }

    @GetMapping("/employee-query")
    @ApiOperation(value = "查询已激活的具有外部联系人权限的企业成员")
    public R<List<EmployeeInheritVO>> getUserByName(){
        List<EmployeeInheritVO> collect = employeeService.list(Wrappers.<WorkEmployeeEntity>lambdaQuery().eq(WorkEmployeeEntity::getStatus, 1).eq(WorkEmployeeEntity::getContactAuth,1).eq(WorkEmployeeEntity::getCorpId, CorpContextHolder.getCorpId())).stream().map(entity -> {
            EmployeeInheritVO employeeInheritVO = new EmployeeInheritVO();
            BeanUtil.copyProperties(entity, employeeInheritVO);
            employeeInheritVO.setDeptId(entity.getMainDepartmentId());
            employeeInheritVO.setStatus(EmployeeStatusEnum.getMsgByCode(entity.getStatus()));
            return employeeInheritVO;
        }).collect(Collectors.toList());
        return R.ok(collect) ;
    }

    @SysLog("在职继承客户")
    @GetMapping("/inherit")
    @ApiOperation(value = "在职继承客户")
    public R changeContact(@RequestParam(value = "oldWxUserId") String oldWxUserId,@RequestParam(value = "newWxUserId")String newWxUserId,@RequestParam(value = "wxExternalUserid")String[] wxExternalUserid){
       return employeeInheritService.updateContact(oldWxUserId,newWxUserId,wxExternalUserid);
    }

    @GetMapping("/resigned")
    @ApiOperation(value = "获取待分配的离职成员对应客户列表")
    public R resignedUserToContact(Page page, String cursor){
        return R.ok(employeeInheritService.resignedUserToContact(page,cursor));
    }

    @SysLog("离职继承")
    @GetMapping("/resigned/inherit")
    @ApiOperation(value = "离职继承")
    public R resignedContactByUser(@RequestParam(value = "oldWxUserId") String oldWxUserId,@RequestParam(value = "newWxUserId")String newWxUserId,@RequestParam(value = "wxExternalUserid")String[] wxExternalUserid){
        return employeeInheritService.resignedContactByUser(oldWxUserId, newWxUserId, wxExternalUserid);
    }

    @SysLog("分配离职成员的客户群")
    @GetMapping("/resigned/group")
    @ApiOperation(value = "分配离职成员的客户群")
    public R resignedGroupToUser(@RequestParam(value = "groupId")String[] groupId,@RequestParam(value = "newWxUserId")String newWxUserId){
        return employeeInheritService.resignedGroupToUser(groupId, newWxUserId);
    }

    @GetMapping("/group")
    @ApiOperation(value = "查询群主已离职的客户群")
    public R groupStatus(Page page){
        return employeeInheritService.groupStatus(page);
    }

}
